package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MenuMapper;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.utils.UserVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImp implements MenuService {
    @Autowired
    MenuMapper menuDao;
    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> menus = menuDao.findByCondition(queryString);

        long total = menus.getTotal();
        List<Menu> result = menus.getResult();
        List<Menu> menu = UserVoUtils.getMenu(result);
        return new PageResult(total, menu);
    }

    @Override
    public List<Menu> findLevel() {
        return menuDao.findLevel();
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer id) {
        return menuDao.findMenuByRoleId(id);
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public String delete(Integer id) {
        try{
        menuDao.delete(id);
        return "删除成功";
        }catch (Exception e){
            return "删除失败,请先和角色解绑,再删除";
        }
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }
}
